package com.example.demo.controller;

import com.example.demo.ServerResponse;
import com.example.demo.entity.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StudentController {
    @Autowired
    StudentService studentService;
    //添加数据
    @PostMapping("/addStudent")
    public Map<String,Object> addStudent(@RequestBody Student stu){
        int i = studentService.addStudent(stu);
        Map<String,Object> map = new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","添加成功");
        }else {
            map.put("code",500);
            map.put("msg","添加失败");
        }
        return map;
    }
    //单个删除
    @DeleteMapping("/deleteStudent")
    public Map<String,Object> deleteStudent(String id){
        int i  = studentService.deleteStudent(id);
        Map<String,Object> map = new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }
    //批量删除
    @PostMapping("/deleteBatchStudent")
    public Map<String,Object> deleteBatchStudent(@RequestBody List<Student> list){
        int i  = studentService.deleteBatchStudent(list);
        Map<String,Object> map = new HashMap<>();
        if(i>=0){
            map.put("code",200);
            map.put("msg","删除成功");
        }else{
            map.put("code",500);
            map.put("msg","删除失败");
        }
        return map;
    }
//    [
//    {"id":"10","name":"Jack","age":28,"image":"inage10.png"},
//    {"id":"11","name":"Karen","age":29,"image":"inage11.png"}
//    ]
    //修改数据
    @PostMapping("/updateStudent")
    public Map<String,Object> updateStudent(@RequestBody Student stu){
        int i = studentService.updateStudent(stu);
        Map<String,Object> map = new HashMap<>();
        if(i>0){
            map.put("code",200);
            map.put("msg","修改成功");
        }else{
            map.put("code",500);
            map.put("msg","修改失败");
        }
        return map;
    }
    //全部查询
    @GetMapping("/getStudent")
    public ServerResponse<List<Student>> getStudent(){
        List<Student> list = studentService.getStudents();
        return ServerResponse.createBySuccess(200,list,"获取成功");
    }
    //部分查询
    @PostMapping("/findStudentByCondition")
    public Map<String,Object> findStudentByCondition(@RequestBody Student student){
        List<Student> list =studentService.findStudentByCondition(student);
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","查询成功");
        map.put("data",list);
        return map;
    }
}
